/* routes */
define('routes', ['angular', 'ngRoute'], function (angular) {

    // 定义路由
    var module = angular.module('routes', ['ngRoute']);
    module.config(["$routeProvider", "$locationProvider", function ($routeProvider, $locationProvider) {


        $routeProvider.when('/', {
            templateUrl:'partials/notice/index.html'
        });
        // 待办事项
        $routeProvider.when('/Notice/index', {
            templateUrl:'partials/notice/index.html'
        });

        // 录入商机
        $routeProvider.when('/Client/client', {
            templateUrl:'partials/client/client.html'
        });
        $routeProvider.when('/Client/client_batch', {
            templateUrl:'partials/client/client_batch.html'
        });
        $routeProvider.when('/Client/client_batch_excel', {
            templateUrl:'partials/client/client_batch_excel.html'
        });
        // 推广管理
        $routeProvider.when('/Client/proportion', {
            templateUrl:'partials/client/proportion.html'
        });
        
        // 商机管理
        $routeProvider.when('/Main/main', {
            templateUrl:'partials/main/main.html'
        });
        $routeProvider.when('/Main/unknown', {
            templateUrl:'partials/main/unknown.html'
        });
        $routeProvider.when('/Main/future', {
            templateUrl:'partials/main/future.html'
        });
        $routeProvider.when('/Main/false_note', {
            templateUrl:'partials/main/false_note.html'
        });
        $routeProvider.when('/Main/success_bus', {
            templateUrl:'partials/main/success_bus.html'
        });
        $routeProvider.when('/Main/main_khxxy/:id/', {
            templateUrl:'partials/main/main_khxxy.html'
        });

        // 销售日报
        $routeProvider.when('/Work/everybody', {
            templateUrl:'partials/Work/everybody.html'
        });
        $routeProvider.when('/Work/froms', {
            templateUrl:'partials/Work/froms.html'
        });
        $routeProvider.when('/Work/work_details/:id/', {
            templateUrl:'partials/Work/work_details.html'
        });

        // 销售计划
        $routeProvider.when('/Sales/index', {
            templateUrl:'partials/Sales/index.html'
        });
        $routeProvider.when('/Sales/person_plan', {
            templateUrl:'partials/Sales/person_plan.html'
        });
        $routeProvider.when('/Sales/sales_person_plan_eidt/:branch_company_id/:month', {
            templateUrl:'partials/Sales/sales_person_plan_eidt.html'
        });

        // 渠道管理
        $routeProvider.when('/Channel/person_channel', {
            templateUrl:'partials/Channel/person_channel.html'
        });
        $routeProvider.when('/Channel/company_channel', {
            templateUrl:'partials/Channel/company_channel.html'
        });
        $routeProvider.when('/Channel/addcollege', {
            templateUrl:'partials/Channel/addcollege.html'
        });

        // 排行管理
        $routeProvider.when('/Son/son', {
            templateUrl:'partials/son/son.html'
        });
        $routeProvider.when('/Son/self', {
            templateUrl:'partials/son/self.html'
        });
        $routeProvider.when('/Son/visit_company', {
            templateUrl:'partials/son/visit_company.html'
        });
        $routeProvider.when('/Son/return_premium_company', {
            templateUrl:'partials/son/return_premium_company.html'
        });
        $routeProvider.when('/Son/return_premium_self', {
            templateUrl:'partials/son/return_premium_self.html'
        });
        $routeProvider.when('/Son/visit_self', {
            templateUrl:'partials/son/visit_self.html'
        });
        $routeProvider.when('/Son/false_self', {
            templateUrl:'partials/son/false_self.html'
        });
        $routeProvider.when('/Son/false_com', {
            templateUrl:'partials/son/false_com.html'
        });

        // 系统管理
        $routeProvider.when('/Manage/manage_people', {
            templateUrl:'partials/manage/manage_people.html'
        });
        $routeProvider.when('/Manage/manage_people_add', {
            templateUrl:'partials/manage/manage_people_add.html'
        });
        $routeProvider.when('/Manage/holiday_set', {
            templateUrl:'partials/manage/holiday_set.html'
        });


        // 登录
        $routeProvider.when('/login', {
            templateUrl:'partials/login/login.html'
        });

        // note that to minimize playground impact on app.js, we
        // are including just this simple route with a parameterized 
        // partial value (see playground.js and playground.html)
        // $routeProvider.when('/playground/:widgetName', {
        //     templateUrl:'playground/playground.html',
        //     controller:'PlaygroundCtrl'
        // });
        
        // 默认
        $routeProvider.otherwise({
            redirectTo:'/'
        });

        // configure html5 to get links working on jsfiddle
        // $locationProvider.html5Mode(true);
        // $locationProvider.hashPrefix('!');
        $locationProvider.hashPrefix('');

    }]).run(['$rootScope', '$location','$window','AuthenticationService',function($rootScope, $location, $window, AuthenticationService) {
        $rootScope.$on("$routeChangeStart", function(event, nextRoute, currentRoute) {
            //redirect only if both isLogged is false and no token is set
            // if (nextRoute !== null && nextRoute.access !== null && nextRoute.access.requiredLogin && !AuthenticationService.isLogged && !$window.sessionStorage.token) {
            if (nextRoute !== null &&  !AuthenticationService.isLogged && !$window.sessionStorage.token) {
                $rootScope.hideUser=false;
                $location.path("/login");
            }else{
                if(!$rootScope.user){
                    $rootScope.user=JSON.parse($window.localStorage.crm_user);
                }
                $rootScope.hideUser=true;
            }
        });
    }]);
});